package com.liliang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liliang.entity.IdMapping;
import com.liliang.entity.MaterialPage;
import com.liliang.entity.Stock;
import com.liliang.entity.StockSupplier;
import com.liliang.service.*;
import com.liliang.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liliang
 * @since 2021-11-04
 */
@Controller
@RequestMapping("/stock")
public class StockController {

    @Autowired
    IStockService iss;

    @Autowired
    IStockSupplierService issls;

    @Autowired
    IIdMappingService iims;

    @Autowired
    IOrdersService ios;

    @Autowired
    ISupplierService isls;

    @PostMapping("/insertStock")
    @ResponseBody
    public boolean insertStock(Stock stock, String cgbzsj, HttpServletRequest request) throws IOException {
        //添加需求计划表
       if(iss.insertStock(stock,cgbzsj)){

           RandomUtil.setliushuihao("cgjh");

           //如果是定向询价则添加 供应商和采购计划关系表
            if (stock.getStockType().equals("C000-2")){
                //得到多个供应商
                String[] nums = request.getParameterValues("nums");
                //生成关系表
                StockSupplier stockSupplier=new StockSupplier();
                //设置采购计划id
                stockSupplier.setStockId(stock.getId());
                for (int i = 0; i <nums.length ; i++) {
                    //设置供应商id
                    stockSupplier.setSupplierId(Long.parseLong(nums[i]));
                    //添加关系
                    issls.save(stockSupplier);
                }
            }

            //添加关系表
           String[] orders = request.getParameterValues("orders");
           IdMapping idMapping=new IdMapping();
           idMapping.setStatus("C001-30");
           idMapping.setStockId(stock.getId());
           for (int i = 0; i <orders.length ; i++) {
               iims.update(idMapping,new QueryWrapper<IdMapping>().eq("ORDER_ID",orders[i]));
           }
            return true;
       }
        return false;
    }

    // 采购计划查询
    @PostMapping("/selectStock")
    @ResponseBody
    public MaterialPage selectStock(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer rows,Stock stock,String materialName,String status){
        return iss.findStockPage((page-1)*rows,rows,stock,materialName,status);
    }

    @PostMapping("/selectStockByStatus")
    @ResponseBody
    public MaterialPage selectStockByStatus(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer rows){
        return iss.selectStockByStatus((page-1)*rows,rows);
    }

    @PostMapping("/selectStockByStatus2")
    @ResponseBody
    public MaterialPage selectStockByStatus2(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer rows,String zt){
        return iss.selectStockByStatus2((page-1)*rows,rows,zt);
    }

    @GetMapping("/findbyID")
    public String findbyID(long ID, Model model){
        model.addAttribute("stock",iss.getById(ID));
        model.addAttribute("orders",ios.findBYstockid(ID));
        model.addAttribute("suppliers",isls.findBYstockid(ID));
        return "/planman/STOCK_findbyid.html";
    }

    @GetMapping("/updatebyID")
    public String updatebyID(long ID, Model model){
        model.addAttribute("stock",iss.getById(ID));
        model.addAttribute("orders",ios.findBYstockid(ID));
        model.addAttribute("suppliers",isls.findBYstockid(ID));
        return "/planman/STOCK_updatebyid.html";
    }

    @PostMapping("/delectByid")
    @ResponseBody
    public boolean delectByid(Integer id){
        if (iss.removeById(id)){
            boolean pd;
            pd=issls.remove(new QueryWrapper<StockSupplier>().eq("STOCK_ID",id));
            pd=iims.remove(new QueryWrapper<IdMapping>().eq("STOCK_ID",id));
            return pd;
        }
        return  false;
    }

    @PostMapping("/updateByorders")
    @ResponseBody
    public boolean updateByorders(Stock stock){
        if (iss.updateById(stock)){
            if (stock.getStockType().equals("C000-1")){
                issls.remove(new QueryWrapper<StockSupplier>().eq("STOCK_ID",stock.getId()));
            }
            IdMapping idMapping=new IdMapping();
            idMapping.setStatus("C001-30");
            return iims.update(idMapping,new QueryWrapper<IdMapping>().eq("STOCK_ID",stock.getId()));
        }

        return false;
    }

}

